package de.wittekind.bundesjugendspiele.server.setup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import crypting.BCrypt;

public class Users {

	public static void addNewUserDialog(Connection c) {
		System.out
				.println("Im folgenden kann der erste Benutzer hinzugefügt werden");
		BufferedReader console = new BufferedReader(new InputStreamReader(
				System.in));
		boolean end = false;
		while (!end) {
			String user = null;
			System.out.println("Username eingeben");
			try {
				user = console.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("Passwort eingeben");
			String password = null;
			try {
				password = console.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("Zusätzlichen Benutzer hinzufügen(Y/n)");
			String weiter = null;
			try {
				weiter = console.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("User " + user + " hinzugefügt");
			try {
				addNewUser(user, password, c);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!"Y".equals(weiter)) {
				end = true;
			}

		}
	}

	public static void addNewUser(String user, String password, Connection c)
			throws SQLException {

		String hashed = BCrypt.hashpw(password, BCrypt.gensalt());
		Statement s = c.createStatement();
		String sql = "insert into users (username,password) values('" + user
				+ "','" + hashed + "')";
		s.execute(sql);
	}
}
